package br.com.dlm.simuladoitil.activities;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import br.com.dlm.simuladoitil.R;
import br.com.dlm.simuladoitil.basicas.Categoria;
import br.com.dlm.simuladoitil.fragments.ListaQuestoesFragment;
import br.com.dlm.simuladoitil.fragments.QuestaoFragment;

public class ListaQuestoesActivity extends GeralActivity implements ListaQuestoesFragment.OnClickQuestaoListener {

    public static final String EXTRA_CATEGORIA = "PARAMETRO_CATEGORIA";
    public static final String TAG_QUESTAO_FRAGMENT = "TAG_QUESTAO_FRAGMENT";
    public static final String VAR_LISTA_QUESTOES_ACTIVITY_LISTA_QUESTOES_FRAGMENT = "VAR_LISTA_QUESTOES_ACTIVITY_LISTA_QUESTOES_FRAGMENT";
    public static final String VAR_LISTA_QUESTOES_ACTIVITY_QUESTAO_FRAGMENT = "VAR_LISTA_QUESTOES_ACTIVITY_QUESTAO_FRAGMENT";

    public static final int REQUEST_CODE_ATUALIZAR_LISTA_QUESTOES = 1;

    private ListaQuestoesFragment listaQuestoesFragment;
    private QuestaoFragment questaoFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lista_questoes);

        listaQuestoesFragment = (ListaQuestoesFragment) getFragmentManager().findFragmentById(R.id.fragmentListaDeQuestoes);

        setResult(RESULT_OK);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        conteudo().putObjeto(VAR_LISTA_QUESTOES_ACTIVITY_LISTA_QUESTOES_FRAGMENT, listaQuestoesFragment);
        conteudo().putObjeto(VAR_LISTA_QUESTOES_ACTIVITY_QUESTAO_FRAGMENT, questaoFragment);
    }

    @Override
    public void clickQuestao(Categoria categoria, int posicao) {
        if (getResources().getBoolean(R.bool.smartphone)) {
            Intent intent = new Intent(this, QuestaoActivity.class);

            intent.putExtra(QuestaoActivity.EXTRA_CATEGORIA, categoria);
            intent.putExtra(QuestaoActivity.EXTRA_POSICAO, posicao);

            startActivityForResult(intent, REQUEST_CODE_ATUALIZAR_LISTA_QUESTOES);
        } else {
            if (conteudo().getObjeto(VAR_LISTA_QUESTOES_ACTIVITY_QUESTAO_FRAGMENT) == null) {
                questaoFragment = QuestaoFragment.newInstance(categoria, posicao);
            } else {
                questaoFragment = (QuestaoFragment) conteudo().getObjeto(VAR_LISTA_QUESTOES_ACTIVITY_QUESTAO_FRAGMENT);
                questaoFragment.carregarInformacoesNaTela(categoria.questoes.get(posicao), posicao);
            }

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.containerQuestao, questaoFragment, TAG_QUESTAO_FRAGMENT);
            fragmentTransaction.commit();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_ATUALIZAR_LISTA_QUESTOES && resultCode == RESULT_CANCELED) {
            listaQuestoesFragment.verificarQuestoesDaRodada();
        }
        if (requestCode == REQUEST_CODE_ATUALIZAR_LISTA_QUESTOES && resultCode == RESULT_OK) {
            listaQuestoesFragment.verificarQuestoesDaNovaRodada();
        }

        conteudo().putObjeto(QuestaoActivity.VAR_QUESTAO_ACTIVITY_QUESTAO_FRAGMENT, null);
    }
}
